package com.hw.protocol.tool.controller;

import cn.hutool.core.util.ReflectUtil;
import com.hw.protocol.tool.config.DecodeContext;
import com.hw.protocol.tool.model.History;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Text;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author : lihy
 * @date : 2024/3/14 9:44
 * description:
 */
@Slf4j
@FXMLController
public class HistoryController {

    @FXML
    public TableColumn tcTime;

    @FXML
    public TableColumn tcIn;

    @FXML
    public TableColumn tcResult;

    @FXML
    public TableColumn tcData;

    @FXML
    public TableView tabHistory;


    public void initialize() {

        tcTime.setCellValueFactory(new PropertyValueFactory("time"));
        tcIn.setCellValueFactory(new PropertyValueFactory("in"));
        tcResult.setCellValueFactory(new PropertyValueFactory("result"));
        tcData.setCellValueFactory(new PropertyValueFactory("data"));

        tcTime.setCellFactory(tc -> tabLineFeed(tcTime));
        tcIn.setCellFactory(tc -> tabLineFeed(tcIn));

        tabHistory.setItems(DecodeContext.historyObs);
    }

    /**
     * 表格单元格换行
     *
     * @param tc 表格列
     * @return 表格单元格
     */
    private TableCell tabLineFeed(TableColumn tc) {
        TableCell<History, String> cell = new TableCell<>();
        Text text = new Text();
        cell.setGraphic(text);
        cell.setPrefHeight(Control.USE_COMPUTED_SIZE);
        text.wrappingWidthProperty().bind(tc.widthProperty());
        text.textProperty().bind(cell.itemProperty());
        return cell;
    }

    @FXML
    public void click(MouseEvent event) {
        //双击事件
        if (event.getClickCount() == 2) {
            TableView.TableViewSelectionModel<History> selectionModel = tabHistory.getSelectionModel();
            if (selectionModel.isSelected(tabHistory.getFocusModel().getFocusedIndex())) {
                int rowIndex = tabHistory.getFocusModel().getFocusedIndex();
                TablePosition<History, ?> tablePosition = tabHistory.getFocusModel().getFocusedCell();

                // 获取单元格的值
                if (tablePosition != null) {
                    try {
                        TableColumn<?, ?> column = tablePosition.getTableColumn();

                        History history = (History) tabHistory.getItems().get(rowIndex);
                        Method getMethod = ReflectUtil.getMethod(History.class,
                                "get" + (column.getId().replace("tc", "")));
                        String cellValue = getMethod.invoke(history).toString();

                        // 打印单元格的值
                        if (cellValue != null) {
                            log.info("Double-clicked cell value: " + cellValue);
                            //复制到剪贴板
                            ClipboardContent content = new ClipboardContent();
                            content.putString(cellValue);
                            Clipboard clipboard = Clipboard.getSystemClipboard();
                            clipboard.setContent(content);
                        }
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
